//
//  StatusCell.swift
//  01- 搭建框架
//
//  Created by apple on 15/10/19.
//  Copyright © 2015年 任伟. All rights reserved.
//

import UIKit

/// 微博 Cell 中控件的间距数值
let StatusCellMargin: CGFloat = 12
/// 微博头像的宽度
let StatusCellIconWidth: CGFloat = 35

/// 微博 Cell
class StatusCell: UITableViewCell {
    
    /// MARK: - 微博视图模型
    var viewModel: StatusViewModel? {
        didSet{
            // 设置工作
            topView.viewModel = viewModel
            
            contentLabel.text = viewModel?.status.text
            
            pictureView.viewModel = viewModel
            // 修改配图视图的大小
            pictureView.snp_updateConstraints { (make) -> Void in
                make.width.equalTo(pictureView.bounds.width)
                make.height.equalTo(pictureView.bounds.height)
                
                // -根据配图的数量 决定配图顶部的间距
                let offset = viewModel?.thumbnailUrls?.count  == 0 ? 0: StatusCellMargin
                make.top.equalTo(contentLabel.snp_bottom).offset(offset)
            }
        }
    }
    
    /// MARK: -根据指定的视图模型计算行高
    func rowHeight(vm: StatusViewModel) -> (CGFloat) {
        
        viewModel = vm
        
        contentView.layoutIfNeeded()
        
        return CGRectGetMaxY(bottomView.frame)
    
    }
    
    /// MARK: -构造函数
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        
        // -取消选中的cell 的反色
        selectionStyle = .None
    
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// MARK: -懒加载控件
    /// 顶部视图
    private lazy var topView: StatusCellTopView = StatusCellTopView()
    /// 微博正文标签
    lazy var contentLabel: UILabel = UILabel(title: "微博正文", fontSize: 14, color: UIColor.darkGrayColor(), screenInset: StatusCellMargin)
    ///配图视图
    lazy var pictureView: StatusCellPictureView = StatusCellPictureView()
    /// 底部视图
    lazy var bottomView: StatusCellBottomView = StatusCellBottomView()
    
}
/// MARK: -设置界面
extension StatusCell {
    
    /// 设置界面
    func setupUI() {
        
        // 设置背景颜色
        //backgroundColor = UIColor.whiteColor()
        
        // 添加控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottomView)
        
        // 自动布局
        //顶部的自动布局
        topView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(2 * StatusCellMargin + StatusCellIconWidth)
        }
        
        // 微博正文标签的自动布局
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left).offset(StatusCellMargin)
            }
        
//        // 微博省略图视图自动布局
//        pictureView.snp_makeConstraints { (make) -> Void in
//            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
//            make.left.equalTo(contentLabel.snp_left)
//            make.width.equalTo(300)
//            make.height.equalTo(90)
//        }
        
        // 微博底部视图的自动布局
        bottomView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(pictureView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(44)
            //make.bottom.equalTo(contentView.snp_bottom)
            
           // 指定向下的约束
           // make.bottom.equalTo(contentView.snp_bottom)
        }
    }
}
 